package br.com.dfr.sgc.util;

import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

/**
 * @author Fabricio
 * 
 * 
 */
public class XmlFileReader {
	private Document document;

	public XmlFileReader(URL fileUrl) throws Exception {
		SAXReader reader = new SAXReader();

		try {
			document = reader.read(fileUrl);
		} catch (DocumentException e) {
			throw new Exception("Could not read XML file. Reason: "
					+ e.getMessage());
		}
	}

	/**
	 * 
	 * @param xPath
	 * @return
	 */
	public String getNodeValue(String xPath) {
		Node node = document.selectSingleNode(xPath);
		if (node != null)
			return node.getText();
		else
			return null;
	}
}
